package de.geraldschenke.tictactoe;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
	public final static String PLAYER1 = "de.geraldschenke.tictactoe.PLAYER1";
	public final static String PLAYER2 = "de.geraldschenke.tictactoe.PLAYER2";
	private Toast mToastText;

	@SuppressLint("ShowToast")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mToastText = Toast.makeText(this, "", Toast.LENGTH_SHORT);

		final Spinner player1select = (Spinner) findViewById(R.id.player1);
		final ArrayAdapter<CharSequence> player1adapter = ArrayAdapter.createFromResource(this,
				R.array.playertype_array, android.R.layout.simple_spinner_item);
		player1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		player1select.setAdapter(player1adapter);
		player1select.setSelection(0);

		final Spinner player2select = (Spinner) findViewById(R.id.player2);
		final ArrayAdapter<CharSequence> player2adapter = ArrayAdapter.createFromResource(this,
				R.array.playertype_array, android.R.layout.simple_spinner_item);
		player2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		player2select.setAdapter(player2adapter);
		player2select.setSelection(0);

	}

	public void displayText(final String message) {
		mToastText.cancel();
		mToastText.setText(message);
		mToastText.show();
	}

	public void startGame(View view) {
		Intent intent = new Intent(this, TicTacToeActivity.class);
		Spinner player1select = (Spinner) findViewById(R.id.player1);
		int player1 = player1select.getSelectedItemPosition();
		Spinner player2select = (Spinner) findViewById(R.id.player2);
		int player2 = player2select.getSelectedItemPosition();

		if (player1 == Spinner.INVALID_POSITION || player2 == Spinner.INVALID_POSITION) {
			displayText(getString(R.string.invalidSelection));
			return;
		}

		intent.putExtra(PLAYER1, player1);
		intent.putExtra(PLAYER2, player2);
		startActivity(intent);
	}
}
